package com.mrd.gtimp.sup.action;

import com.mrd.constant.MsgConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.config.web.json.annotation.JField;
import com.mrd.framework.config.web.json.annotation.Json;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.Filter;
import com.mrd.framework.ui.filter.FilterGroupOption;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.framework.utils.HttpUtils;
import com.mrd.gtimp.base.entity.Company;
import com.mrd.gtimp.sup.entity.SupShipMentInfo;
import com.mrd.gtimp.sup.service.SupShipMentInfoBiz;
import com.mrd.util.LoginUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author luob
 */

@Controller
@RequestMapping("/sup")
public class SupShipMentInfoAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private SupShipMentInfoBiz supShipMentInfoBiz;

    /**
     * public String show() {
     * try {
     * page = initPage(SupShipMentInfoBiz.findEntityCount(filters));
     * Order order = new Order();
     * order.add(sidx, sord);
     * SupShipMentInfos = SupShipMentInfoBiz
     * .findEntityByFilter(page.getCurrentPageStartNo(), page.getPageSize(), filters, order);
     * } catch (Exception e) {
     * e.printStackTrace();
     * }
     * return SUCCESS;
     * }
     **/

    /**
     * 默认访问入口
     */
    @RequestMapping("/supShipMentInfo-main")
    public String main() {
        return "/gtimp/sup/supShipMentInfoList";
    }

    /**
     * 默认新增修改入口
     */
    @RequestMapping("/supShipMentInfo-manage")
    public String manage(String id, HashMap map) {
        map.put("id", id);
        return "/gtimp/sup/supShipMentInfoManage";
    }

    /**
     * 新增new入口
     */
    @RequestMapping("/supShipMentInfo-save")
    public String save() {
        return "/gtimp/sup/supShipMentInfoSave";
    }

    /**
     * 新增或修改信息
     */
    //// TODO: 2016/8/15   @Permissions
//    @Permissions(permissionsId = RoleConstants.SUP_SHIPMENTINFO_SAVE)
    @RequestMapping("/supShipMentInfo-saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(SupShipMentInfo supShipMentInfo) {
        Result msg;
        // if(checkRight(RoleConstants.SUP_SHIPMENTINFO_SAVE)) {
        try {
            if (StringUtils.isEmpty(supShipMentInfo.getID())) {  // 新增操作
                Company owner = new Company();
                owner.setId(LoginUtil.getLoginUser().getCompany().getId());
                supShipMentInfo.setOwner(owner);
                supShipMentInfo.setCREATOR(LoginUtil.getLoginUser().getAccount());
                supShipMentInfo.setCTIME(new Date());
                supShipMentInfo.setID(null);
            } else {  // 修改操作
                supShipMentInfo.setMODIFIER(LoginUtil.getLoginUser().getAccount());
                supShipMentInfo.setMTIME(new Date());
            }
            supShipMentInfoBiz.saveOrupdateEntity(supShipMentInfo);
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL + e.getMessage());
        }
        return msg;
    }

    /**
     * 编辑信息
     */
    @RequestMapping("/supShipMentInfo-find")
    @ResponseBody
    public SupShipMentInfo find(SupShipMentInfo supShipMentInfo) {
        if (null != supShipMentInfo) {
            if (StringUtils.isNotEmpty(supShipMentInfo.getID())) {
                try {
                    supShipMentInfo = supShipMentInfoBiz.findEntityById(supShipMentInfo.getID());
                    return supShipMentInfo;
                } catch (QueryException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 删除信息
     */
    //// TODO: 2016/8/15   @Permissions
//    @Permissions(permissionsId = RoleConstants.SUP_SHIPMENTINFO_DEL)
    @RequestMapping("/supShipMentInfo-del")
    public Result del() {
        Result msg;
        // if(checkRight(RoleConstants.SUP_SHIPMENTINFO_DEL)) {
        try {
            Filter filters = null;
            String ids = HttpUtils.getRequest().getParameter("id");
            String[] idArr = ids.split(",");
            StringBuffer sb = new StringBuffer();
            for (String string : idArr) {
                sb.append("'").append(string).append("',");
            }
            if (sb.length() > 0) {
                sb.deleteCharAt(sb.length() - 1);
                filters = new Filter();
                filters.setGroupOp(FilterGroupOption.AND);
                filters.addRule(new SearchRule("ID", SearchRuleOption.in, sb.toString()));
                supShipMentInfoBiz.deleteByFilter(filters);
            }
            msg = Result.newSuccessInstance(MsgConstants.DEL_SUCCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.DEL_FAIL + e.getMessage());
        }
        return msg;
    }

    /**
     * 查询
     */
    @RequestMapping("/supShipMentInfo-findList")
    @ResponseBody
    @Json({@JField("ID"), @JField("shipCompany.name"), @JField("LINK")})
    public Page<SupShipMentInfo> findList(Jqgrid jqgrid) {
        String comId = LoginUtil.getLoginUser().getCompany().getId();// get from user
        try {
            SearchRule rule = new SearchRule("owner.id", SearchRuleOption.eq, comId);
            jqgrid.addRule(rule);
//            if (null != sidx && !"".equals(sidx)) {
//                order.add(sidx, sord);
//            }
//            supShipMentInfos = supShipMentInfoBiz
//                    .findEntityByFilter(page.getCurrentPageStartNo(), page.getPageSize(), filters, order);
            Page<SupShipMentInfo> supShipMentInfos = supShipMentInfoBiz.findByJqgrid(jqgrid);
            return supShipMentInfos;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    @Resource
    public void setSupShipMentInfoBiz(SupShipMentInfoBiz supShipMentInfoBiz) {
        this.supShipMentInfoBiz = supShipMentInfoBiz;
    }
}
